<template>
	<view class="container">
		<u--form :model="form" ref="uForm" :rules="rules" labelWidth="0">
			<view class="form-subtitle">
				<view class="form-subtitle-left">
					基本信息
				</view>
			</view>
			<view class="form-container">
				<u-form-item prop="name">
					<view class="form-flex">
						<span class="form-label">姓名<span class="label-required">*</span></span>
						<u--input v-model="form.name" placeholder="请输入姓名" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item>
					<view class="form-flex">
						<span class="form-label">性别</span>
						<u-radio-group v-model="form.sex" activeColor="#FE4F04" class="u-radio-group--row">
							<u-radio name="1" label="男"> 男 </u-radio>
							<u-radio name="2" label="女"> 女 </u-radio>
						</u-radio-group>
					</view>
				</u-form-item>
				<u-form-item>
					<view class="form-flex">
						<span class="form-label">手机号<span class="label-required">*</span></span>
						<u--input v-model="form.mobile" placeholder="请输入手机号" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item>
					<view class="form-flex">
						<span class="form-label">证件号码</span>
						<u--input v-model="form.id_card" placeholder="请输入证件号码" inputAlign="right" border="none" />
					</view>
				</u-form-item>
				<u-form-item>
					<view class="form-flex">
						<span class="form-label">邮箱</span>
						<u--input v-model="form.email" placeholder="请输入邮箱" inputAlign="right" border="none" />
					</view>
				</u-form-item>

				<u-form-item>
					<view class="form-flex">
						<span class="form-label">角色<span class="label-required">*</span></span>
						<u-radio-group v-model="form.role" activeColor="#FE4F04">
							<u-radio name="1" label="房东"> 房东 </u-radio>
							<u-radio name="2" label="楼管"> 楼管 </u-radio>
						</u-radio-group>
					</view>
				</u-form-item>

				<u-form-item>
					<view class="form-flex">
						<span class="form-label">备注</span>
						<u-input v-model="form.remark" placeholder="请输入备注" inputAlign="right" border="none" />
					</view>
				</u-form-item>
			</view>

			<view class="form-subtitle">
				<view class="form-subtitle-left">
					所属网点
				</view>
			</view>
			<view class="form-container">
				<u-form-item>
					<view class="form-flex">
						<u-checkbox-group v-model="form.build_ids" placement="column">
							<u-checkbox activeColor="#FE4F04" :name="build.id" :label="build.address" v-for="(build,index) in addressListData" :key="build.id"></u-checkbox>
						</u-checkbox-group>
						<!-- <span class="form-label">所属网点</span> -->
						<!-- <u-input v-model="form.build_ids" placeholder="请输入所属网点" inputAlign="right" border="none" /> -->
					</view>
				</u-form-item>
			</view>

			<view class="form-bottom-opt">
				<view class="opt-del" v-if="form.id && form.id != ''" @click="handleDel">
					<img src="@/pages-landlord/static/images/del.png" alt="" class="opt-icon"/>
					<span>删除</span>
				</view>
				<view class="opt-del" v-if="form.id && form.id != ''" @click="handleLog">
					<!-- <img src="@/pages-landlord/static/images/del.png" alt="" class="opt-icon"/> -->
					<u-icon name="more-circle" size="17"></u-icon>
					<span>登录日志</span>
				</view>
				<u-button v-if="!form.id" @click="submit" :custom-style="submitBtnStyle">提交</u-button>
			</view>
		</u--form>
		
		<u-modal :show="delShow" title="确认要删除此员工吗？" :showCancelButton="true" @cancel="delShow = false" @confirm="delConfirm"
			confirmColor="#FE4F04"></u-modal>
	</view>
</template>


<script>
	import {
		staffAdd,
		staffInfo,
		staffDel
	} from "@/api/part2"
		import { addressList } from "@/api/landlord.js";
	export default {
		data() {
			return {
				form: {
					id: '',
					name: '',
					sex: '',
					mobile: '',
					id_card: '',
					role: '',
					remark: '',
					email: '',
					build_ids: [],
				},
				rules: {
					'name': {
						type: 'string',
						required: true,
						message: '请填写姓名',
						trigger: ['blur', 'change']
					},
					'type': {
						type: 'string',
						required: true,
						message: '请选择类型',
						trigger: ['change']
					},
					'mobile': {
						type: 'string',
						required: true,
						message: '请填写手机号',
						trigger: ['blur']
					},
				},
				addressListData:[],
				
				delShow:false,
				
				submitBtnStyle: {
					background: "#FE4F04",
					boShadow: "0rpx 4rpx 8rpx 0rpx rgba(254,79,4,0.5)",
					borderRadius: "44rpx",
					fontSize: "30rpx",
					color: "#FFFFFF",
					height: "88rpx",
				}
			};
		},
		onLoad(options) {
			if (options.id) {
				this.form.id = options.id
				this.getstaffInfo()
			}
			this.addressList()
		},
		methods: {
			addressList(){
				addressList().then(res=>{
					this.addressListData = res.data.data
				})
			},
			getstaffInfo() {
				staffInfo({id:this.form.id}).then(res => {
					console.log(res.data.data)
					let data = res.data.data
					this.form = {
						...data,
						sex:data.sex+'',
						build_ids: data.build_ids== ''? []: data.build_ids.split(',').map(b=>{
							return Number(b)
						})
					}
				})
			},
			submit() {
				console.log('提交')
				this.$refs.uForm.validate().then(res => {
					staffAdd({...this.form,build_ids:this.form.build_ids.join(',')}).then(res => {
						uni.$u.toast(res.msg || '添加成功')
						setTimeout(() => {
							uni.navigateBack()
						}, 800)
					})
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			handleDel() {
				this.delShow = true
			},
			delConfirm() {
				staffDel({
					id: this.form.id
				}).then(res => {
					uni.$u.toast(res.msg || '删除成功')
					setTimeout(() => {
						uni.navigateBack()
					}, 800)
				})
			},
			handleLog(){
				uni.navigateTo({
					url:'/pages-landlord/employee/log?id=' + this.form.id
				})
			}
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		}
	};
</script>


<style lang="less" scoped>
	.container {
		padding: 0 32rpx;
		padding-bottom: 150rpx;
		margin-bottom: constant(safe-area-inset-bottom);
		margin-bottom: env(safe-area-inset-bottom);
		background: #F5F6FA;
		min-height: 100vh;
		box-sizing: border-box;
	}

	.form-subtitle {
		padding: 24rpx 40rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 700;
		font-size: 26rpx;
		color: #2A0C00;
		line-height: 36rpx;

		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.form-container {
		background: #FFFFFF;
		box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(0, 0, 0, 0.01);
		border-radius: 16rpx;
		padding: 0 40rpx;
		margin-bottom: 20rpx;

		.form-flex {
			display: flex;
			justify-content: space-between;
			align-items: center;
		}

		.form-label {
			margin-right: 10rpx;

			.label-required {
				color: #FF0000;
				padding: 0 4rpx;
			}
		}
	}

	.form-bottom-opt {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		z-index: 1000;
		/* 关键样式 - 使用 constant 和 env 兼容不同设备 */
		padding-bottom: constant(safe-area-inset-bottom);
		/* 兼容 iOS < 11.2 */
		padding-bottom: env(safe-area-inset-bottom);
		/* 兼容 iOS >= 11.2 */
		/* 其他样式按需添加 */
		background-color: #FFF;
		padding: 20rpx 40rpx;
		
		display: flex;
		gap: 48rpx;
		justify-content: space-around;
		
		.opt-icon {
			width: 32rpx;
			height: 32rpx;
		}
		
		.opt-del {
			min-width: 44rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 22rpx;
			color: #333333;
			line-height: 32rpx;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			height: 44px;
		}
		
	}
	

	/deep/ .u-form-item {
		justify-content: flex-end !important;
		/* 右对齐 */
	}

	/deep/ .u-radio-group--row {
		gap: 30rpx;
		justify-content: flex-end;
		align-items: center;
	}
	/deep/ .u-checkbox-group--column{
		gap: 20rpx;
	}

</style>